package com.dts.evaluate.ui.activity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dts.evaluate.R;
import com.dts.evaluate.bean.FlowDataBean;
import com.dts.evaluate.bean.ProjectDataBean;
import com.dts.evaluate.callback.BeanCallBack;
import com.dts.evaluate.common.Constants;
import com.dts.evaluate.controller.impl.DataImpl;
import com.dts.evaluate.controller.intf.DataInterface;
import com.dts.evaluate.ui.view.FlowLayout;
import com.dts.evaluate.ui.view.TitleView;
import com.dts.evaluate.utils.DensityUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class FlowActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TitleView titleView;
    @Bind(R.id.flow_layout)
    FlowLayout flowLayout;
    @Bind(R.id.activity_flow)
    LinearLayout activityFlow;
    @Bind(R.id.none_flow_textvieww)
    TextView noneFlowTextvieww;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flow);
        ButterKnife.bind(this);

        //设置titleview返回监听
        titleView.setOnBackClickListener(new TitleView.OnBackClikListener() {
            @Override
            public void onTitleViewClik(View v) {
                finish();
            }
        });

        ProjectDataBean.ApproveProjectBean approveProjectBean = (ProjectDataBean.ApproveProjectBean) getIntent().getSerializableExtra(Constants.APPROVE_PROJECT_BEAN);
        if (approveProjectBean == null) {
            return;
        }

        int id = approveProjectBean.getId();


        //办事流程
        DataInterface dataInterface = new DataImpl();
        dataInterface.getKbsxData(Constants.KBSX_URL, Constants.TYPE_THING_PROCESS, String.valueOf(id), new BeanCallBack<FlowDataBean>() {

            @Override
            public void onResponse(boolean isFromCache, FlowDataBean flowDataBean, Request request, @Nullable Response response) {
                if (flowDataBean == null) {
                    return;
                }
                List<FlowDataBean.FlowinfoBean> data = flowDataBean.getFlowinfo();
                if (data == null || data.size() == 0) {
                    flowLayout.setVisibility(View.GONE);
                    noneFlowTextvieww.setVisibility(View.VISIBLE);
                    return;
                }
                bindDataToFlowLayout(data);
            }
        }, this);
    }


    /**
     * 将数据绑定到加载到flowLayout中
     *
     * @param data
     */
    private void bindDataToFlowLayout(List<FlowDataBean.FlowinfoBean> data) {
        for (int i = 0; i < data.size(); i++) {
            FlowDataBean.FlowinfoBean flowinfoBean = data.get(i);

            String flowName = flowinfoBean.getFlowname() + "/";
            int intMaxDay = flowinfoBean.getMaxday();
            String maxDay = null;
            if (intMaxDay == 0) {
                maxDay = getResources().getString(R.string.current_day);
            } else {
                maxDay = intMaxDay + getResources().getString(R.string.day);
            }


            SpannableString serialNumber = new SpannableString(String.valueOf(i + 1) + getResources().getString(R.string.one_point));
            serialNumber.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, serialNumber.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            SpannableStringBuilder builder = new SpannableStringBuilder(serialNumber);

            int serialLenth = builder.length();
            builder.append(flowName);
            builder.setSpan(new ForegroundColorSpan(Color.WHITE), serialLenth, builder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            int flowNameLength = builder.length();
            builder.append(maxDay);
            builder.setSpan(new RelativeSizeSpan(0.5f), flowNameLength, builder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(this, 120));
            ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(layoutParams);
            lp.setMargins(10, 10, 10, 10);
            TextView tv = new TextView(this);
            tv.setLayoutParams(lp);
            tv.setPadding(30, 0, 30, 0);
            tv.setGravity(Gravity.CENTER);
            tv.setText(builder);
            tv.setTextColor(Color.parseColor("#aaF6F9E0"));
            tv.setTextSize(DensityUtil.sp2px(this, 20));
            tv.setBackgroundResource(R.color.resume_button_bg_color);

            flowLayout.addView(tv);

            if (i == data.size() - 1) {
                break;
            }

            ViewGroup.LayoutParams layoutParams1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(this, 100));
            ViewGroup.MarginLayoutParams lp1 = new ViewGroup.MarginLayoutParams(layoutParams);
            lp1.setMargins(20, 10, 20, 10);
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(lp1);
            imageView.setImageResource(R.mipmap.arrow);

            flowLayout.addView(imageView);

        }
    }

   /* static class GridViewAdapter extends BaseAdapter {

        private Context context;
        private LayoutInflater inflater;
        private List<FlowDataBean.FlowinfoBean> data = new ArrayList<>();

        public static final int HAS_ARROW = 1;
        public static final int NOT_ARROW = 0;

        public GridViewAdapter(Context context) {
            this.context = context;
            inflater = LayoutInflater.from(context);
        }

        public void setData(List<FlowDataBean.FlowinfoBean> data) {
            this.data = data;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return getItem(position).hashCode();
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == getCount() - 1) {
                return NOT_ARROW;
            }
            return HAS_ARROW;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            int itemType = getItemViewType(position);
            FlowDataBean.FlowinfoBean flowinfoBean = (FlowDataBean.FlowinfoBean) getItem(position);

            HasArrowViewHolder hasArrowViewHolder = null;
            NotArrowViewHolder notArrowViewHolder = null;
            if (convertView == null) {
                switch (itemType) {
                    case HAS_ARROW:
                        convertView = inflater.inflate(R.layout.flow_grid_item_has_arrow_layout, null);
                        hasArrowViewHolder = new HasArrowViewHolder(convertView);
                        convertView.setTag(R.layout.flow_grid_item_has_arrow_layout, hasArrowViewHolder);
                        break;
                    case NOT_ARROW:
                        convertView = inflater.inflate(R.layout.flow_grid_item_not_arrow_layout, null);
                        notArrowViewHolder = new NotArrowViewHolder(convertView);
                        convertView.setTag(R.layout.flow_grid_item_not_arrow_layout, notArrowViewHolder);
                        break;
                }
            } else {
                switch (itemType) {
                    case HAS_ARROW:
                        hasArrowViewHolder = (HasArrowViewHolder) convertView.getTag(R.layout.flow_grid_item_has_arrow_layout);
                        break;
                    case NOT_ARROW:
                        notArrowViewHolder = (NotArrowViewHolder) convertView.getTag(R.layout.flow_grid_item_not_arrow_layout);
                        break;
                }
            }

            String flowName = flowinfoBean.getFlowname() + "/";
            String maxDay = flowinfoBean.getMaxday() + "天";


            SpannableString serialNumber = new SpannableString(String.valueOf(position + 1) + ".");
            serialNumber.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, serialNumber.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            SpannableStringBuilder builder = new SpannableStringBuilder(serialNumber);

            int serialLenth = builder.length();
            builder.append(flowName);
            builder.setSpan(new ForegroundColorSpan(Color.WHITE), serialLenth, builder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            int flowNameLength = builder.length();
            builder.append(maxDay);
            builder.setSpan(new RelativeSizeSpan(0.5f), flowNameLength, builder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            switch (itemType) {
                case HAS_ARROW:
                    hasArrowViewHolder.textview.setText(builder);
                    break;
                case NOT_ARROW:
                    notArrowViewHolder.textview.setText(builder);
                    break;
            }

            return convertView;
        }

        static class HasArrowViewHolder {
            @Bind(R.id.textview)
            TextView textview;

            HasArrowViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

        static class NotArrowViewHolder {
            @Bind(R.id.textview)
            TextView textview;

            NotArrowViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }*/
}
